﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PRO_DOCS.BLL.WIN.Authentication
{
    public static class LOGGED_IN_EMPLOYEE_DETAILS
    {
        public static DAL.Database.PRO_DOCSDatabaseDataContext PRO_DOCSDatabaseDataContext
        {
            get { return new DAL.Database.PRO_DOCSDatabaseDataContext(DAL.Properties.Settings.Default.PRO_DOCSConnectionString); }
        }

        public static DAL.Database.LOGIN_USERS_SETTING LOGGED_IN_USER_SETTINGS
        {
            get;
            set;
        }

        private static DAL.Database.LOGIN_USER _lOGIN_USER;

        /// <summary>
        /// Gets the login user.
        /// </summary>
        /// <value>The login user.</value>
        public static DAL.Database.LOGIN_USER LOGIN_USER
        {
            get { return _lOGIN_USER; }
        }

        private static bool _iS_AUTHENTICATED_USER;
        /// <summary>
        /// Gets a value indicating whether this instance is authenticated user.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is authenticated user; otherwise, <c>false</c>.
        /// </value>
        public static bool IS_AUTHENTICATED_USER
        {
            get { return _iS_AUTHENTICATED_USER; }
            set { _iS_AUTHENTICATED_USER = value; }
        }
        public static Dictionary<long, DAL.Database.SYSTEM_ROLE> CURRENT_LOGGED_IN_USER_ROLES
        { get; set; }
        /// <summary>
        /// Signs the in user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        public static bool SignInUser(string userName, string password)
        {
            //decrypt the password
            password = ProCodes.Utilities.TextEncryption.Encrypt(password);
            _lOGIN_USER = DAL.AppCode.LoginUsers.GetSignInDetails(userName, password);

            if (LOGIN_USER != null)
            {
                IS_AUTHENTICATED_USER = true;

                //get the user roles
                CURRENT_LOGGED_IN_USER_ROLES = LOGIN_USER.LOGIN_USER_ROLEs.ToDictionary(k => k.ROLE_ID, v => v.SYSTEM_ROLE);

                //load the archives security
                LOGGED_IN_EMPLOYEE_ARCHIVES_SECURITY.LOAD_LOGGED_IN_ARCHIVES_SECURITY_SETTINGS();

                // get user settings
                LOGGED_IN_USER_SETTINGS = (from settings in PRO_DOCSDatabaseDataContext.LOGIN_USERS_SETTINGs
                                           where settings.LOGIN_USER_ID.Equals(LOGIN_USER.LOGIN_USER_ID)
                                           select settings).FirstOrDefault();

                if (LOGGED_IN_USER_SETTINGS != null)
                {
                    //get the last language details and other settings
                }
                else
                {

                    //1. add the default settings
                    PRO_DOCSDatabaseDataContext.proc_LOGIN_USERS_SETTINGSInsert
                        (
                            LOGIN_USER.LOGIN_USER_ID,
                            DateTime.Now,
                            70
                        );
                    //2. reload
                    LOGGED_IN_USER_SETTINGS = (from settings in PRO_DOCSDatabaseDataContext.LOGIN_USERS_SETTINGs
                                               where settings.LOGIN_USER_ID.Equals(LOGIN_USER.LOGIN_USER_ID)
                                               select settings).FirstOrDefault();

                    if (LOGGED_IN_USER_SETTINGS != null)
                    {
                        //get the last language details and other settings

                    }
                }

                //Add login history

                LoggedInEmployeeSession.OPEN_LOGGED_IN_USER_SESSION();

            }
            else
            {
                //LOGIN_USER_ROLES = new Dictionary<long, DAL.Database.LOGIN_USER_ROLE>();
            }

            return IS_AUTHENTICATED_USER;
        }

        /// <summary>
        /// Signs the out user.
        /// </summary>
        public static void SignOutUser()
        {
            _iS_AUTHENTICATED_USER = false;
            _lOGIN_USER = null;
        }
    }
}
